package dk.android.houseenabler.classes;

import java.math.BigInteger;

import dk.android.houseenabler.models.Answer;
import dk.android.houseenabler.models.House;
import dk.android.houseenabler.models.HouseCollection;
import dk.android.houseenabler.models.Person;

public class ProfilesExporter {

	
	public String exportEnvironmentProfiles(House house) {
		String hex = this.makeEnvironmentProfile(house);
		
		byte[] b = hexStringToByteArray(hex);
		String filename = this.saveFile(HouseEnablerSettings.ENVIRONMENTFILENAME, b);
		
		return filename;
	}
	
	public String exportEnvironmentProfiles(HouseCollection houses) {
		String hex = "";
		for(House h : houses.getHouses()) {
			hex += this.makeEnvironmentProfile(h);
		}
		
		byte[] b = hexStringToByteArray(hex);
		String filename = this.saveFile(HouseEnablerSettings.ENVIRONMENTFILENAME, b);
		
		return filename;
	}
	
	public String exportFunctionProfiles(Person person) {
		String hex = this.makeFunctionProfile(person);
		
		byte[] b = hexStringToByteArray(hex);
		String filename = this.saveFile(HouseEnablerSettings.FUNCTIONFILENAME, b);
		
		return filename;
	}
	
	public String makeEnvironmentProfile(House house) {
		String theHex = null;
		if(house != null) {
			theHex = "";
			
			String title = house.getTitle();
			// The title can't be over maxNameLength!
			if(title.length() > HouseEnablerSettings.MAXIMUMNAMELENGTH) {
				title = title.substring(0, HouseEnablerSettings.MAXIMUMNAMELENGTH);
			}
			
			// Converting the string length to hex
			String lengthString = Integer.toHexString(title.length());
			// Adding a zero, if the file is only 1 diget long
			if(lengthString.length() == 1) {
				lengthString = "0" + lengthString;
			}
			
			theHex += lengthString;
			theHex += toHex(title);
			
			// Putting empty places (00) after the name up to maxNameLength
			for(int i = title.length() ; i < HouseEnablerSettings.MAXIMUMNAMELENGTH ; i++) {
				theHex += "00";
			}
			
			char[] letters = {'a', 'b', 'c', 'd'};
			for(char c : letters) {
				for(Answer a : house.getAnswers().getAnswers(c)) {
					theHex += a != null ? (a.isChecked() ? "01" : "00") : "00";
				}
			}
		
		}
		
		return theHex;
	}
	
	public String makeFunctionProfile(Person person) {
		String theHex = null;
		if(person != null) {
		
			theHex = "";
			
			String title = person.getTitle();
			// The title can't be over maxNameLength!
			if(title.length() > HouseEnablerSettings.MAXIMUMNAMELENGTH) {
				title = title.substring(0, HouseEnablerSettings.MAXIMUMNAMELENGTH);
			}
			
			// Converting the string length to hex
			String lengthString = Integer.toHexString(title.length());
			// Adding a zero, if the file is only 1 diget long
			if(lengthString.length() == 1) {
				lengthString = "0" + lengthString;
			}
			
			theHex += lengthString;
			theHex += toHex(title);
			
			// Putting empty places (00) after the name up to maxNameLength
			for(int i = title.length() ; i < HouseEnablerSettings.MAXIMUMNAMELENGTH ; i++) {
				theHex += "00";
			}
			
			// 15 profiles
			for(Answer a : person.getAnswers()) {
				theHex += a != null ? (a.isChecked() ? "01" : "00") : "00";
			}
			
			// Adding some "spam" to the end of the profile (don't ask why (maybe because House Enabler is a morron) we just have to!!)
			theHex += "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000161601000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000";
		}
		
		return theHex;
	}
	
	private String saveFile(String filename, byte[] bytes) {
		FileHandle fh = new FileHandle();
		fh.writeDataToStorage(filename, bytes);
		
		return filename;
	}
	
	private String toHex(String arg) {
	    return String.format("%x", new BigInteger(arg.getBytes()));
	}
	
	private byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    
	    // Taking every 2 chars and byteshift them
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    
	    return data;
	}
}
